//index.js
const Controllers = require('../controllers/Controller.js').controller;
const app = getApp();
Page({
  data: {
    globalStype:'',
    tabbar:[],
    shopId:'',
    shopDetailsdata:'',
    shopRotationImgdata:'',
    imgList2:[],
    imgheights: [],
    //图片宽度 
    imgwidth: 750,
    //默认  
    current: 0,
  },
  onLoad(option) {
    this.setData({globalStype:app.globalData.globalStype})
    this.setData({
      tabbar: this.data.globalStype=='pinStyle'?app.globalData.tabbar:app.globalData.tabbar2,
    })
    if(app.globalData.shopId){
      this.setData({
        shopId:app.globalData.shopId
      })
      this.shopDetails()
    }
  },
  imageLoad: function (e) {//获取图片真实宽度  
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比  
      ratio = imgwidth / imgheight;
      console.log(imgwidth, imgheight)
    //计算的高度值  
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight;
    var imgheights = this.data.imgheights;
    //把每一张图片的对应的高度记录到数组里  
    imgheights[e.target.dataset.id] = imgheight;
    this.setData({
      imgheights: imgheights
    })
  },
  tabChange(e){
    var index=e.detail.index;
    if(index==0){
      // wx.reLaunch({
      //   url: "/pages/Index/Index"
      // });

      wx.requestSubscribeMessage({
        tmplIds:['CgSkX1UN123rlfAQ9zXiMyfiF1ugDkOsd8yLmvcbHt0','IoewIlKNaUfOLOhKUOdPKMOnJxdtdqWGfOotuOB2R70','TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'],//'TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'
        success:res=>{
          console.log(res)
        },
        complete:()=>{
          wx.reLaunch({
            url: "/pages/Index/Index"
          });
        }
      })
    }
    if(index==1){
      wx.reLaunch({
        url: "/pages/Mall/Mall"
      });
    }
    if(index==2){
      wx.reLaunch({
        url: "/pages/cart/index/index"
      });
    }
    if(index==3){
      wx.reLaunch({
        url: "/pages/my/my-index/my-index"
      });
    }
  },
  shopDetails(){
    //获取数据
    Controllers.shopDetails({shopId:this.data.shopId}).then(res => {
      let imgList2 =[]
      if(res.data.label){
        res.data.label=res.data.label.split('/')
      }
      if(res.data.imgList.length>0){

        for(let item of res.data.imgList){
          imgList2.push(item.image)
        }
        
      }
      this.setData({
        shopDetailsdata:res.data,
        imgList2 
      })
      this.shopRotationImg()
 
    })
  },
  shopRotationImg(){
    //获取数据
    Controllers.shopRotationImg({shopId:this.data.shopId,type:6}).then(res => {
      this.setData({
        shopRotationImgdata:res.data
      })
    })
  },
  calling(e){
    var phone=e.currentTarget.dataset.phone
    app.calling(phone)
  },
  location(e){
    var lng=e.currentTarget.dataset.lng;
    var lat=e.currentTarget.dataset.lat;
    var name=e.currentTarget.dataset.name;
    var adress=e.currentTarget.dataset.adress;
    wx.openLocation({
    //当前经纬度
      latitude: Number(lat),
      longitude: Number(lng),
      //缩放级别默认28
      scale: 28,
      //位置名
      name: name,
      //详细地址
      address: adress,
      //成功打印信息
      success: function(res) {
          console.log(res)
      },
      //失败打印信息
      fail: function(err) {
          console.log(err)
      },
      //完成打印信息
      complete: function(info) {
          console.log(info)
      },
    })
  },
  preview(event) {
    let currentUrl = event.currentTarget.dataset.src
    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: this.data.imgList2 // 需要预览的图片http链接列表
    })
  },

})